<template>
	<view class="">
		<w-navbar :isBack="false">
			<template #w-navbar-title>
				<view class="top_box">
					<view class="left">
						<image src="../../static/image/logo.png" mode=""></image>
					</view>
					<view class="right">
						<input @input="search_goods" v-model="search_title" type="text" placeholder="搜索">
						<view @click="search_goods" class="iconfont icon-sousuo"></view>
					</view>
				</view>
			</template>
		</w-navbar>
		<w-container component="view" isTabbar>
			<template>
				<view class="content">
					<view class="left">
						<view @click="categoryChoose(item)" class="class_item" v-for="(item,index) in category_list" :key="index">
							<view :class="category_choose.id == item.id?'class_item_check':''" class="class_item_name ">
								{{item.name}}
							</view>
							<view :class="category_choose.id == item.id?'class_line_check':''" class="line"></view>
						</view>
					</view>
					<view class="right">
						<view class="class_name">
							{{category_choose.name}}
						</view>
						<view v-if="goods_list && goods_list.length" class="frw">
							<view @click="goGoodsDetail(item)" class="goods_item fc-jc" v-for="(item,index) in goods_list" :key="item.id">
								<view class="goods_img">
									<image :src="ossURL + item.image" mode="widthFix"></image>
								</view>
								<view class="goods_name">
									{{item.title}}
								</view>
								<view class="fr-ac-jsb" style="margin-top: auto;">
									<view class="goods_price">
										￥{{item.price}}
									</view>
									<view v-if="is_edit_goods" @click.stop="goGoodsEdit(item)" class="iconfont icon-tianxie-copy"></view>
								</view>
							</view>
						</view>
						
						<template v-else>
							<view class="fr-ac-jc" style="width: 100%;height: 80%;">
								<u-loadmore status="nomore" nomoreText="暂无数据" />
							</view>
						</template>
					</view>
				</view>
			</template>
		</w-container>
		<w-tabbar></w-tabbar>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				search_title:'',
				
				category_list:[{
					id:0,
					name:'全部',
				}],
				category_choose:{
					id:0,
					name:'全部',
				},
				goods_list:[],
				
				is_edit_goods:'',
				is_see_special:'',
				price_level:'',
			}
		},
		watch: {
		
		},
		async onLoad() {
			await this.get_category_index()
			await this.search_goods()
		},
		onShow() {
		},
		methods: {
			async get_category_index() {
				uni.showLoading({
					title: '飞速加载中...'
				})
				const res = await this.$http({
					url: '/addons/shop/api.category/index',
					data: {}
				})
				uni.hideLoading()
				if (res.code == 1) {
					this.is_edit_goods = res.data.is_edit_goods //是否展示编辑按钮

					this.is_see_special = res.data.is_see_special //是否查看特殊商品
					
					this.price_level = res.data.price_level //价格等级

					this.category_list = this.category_list.concat(res.data.list)
					if(this.category_list.length){
						this.category_choose = this.category_list[0]
						this.goods_list = this.category_list[0].goods
					}
					
				}else{
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
				}
			},
			async search_goods() {
				let source
				if(this.search_title == ''){
					source = 0
				}else{
					this.category_choose = this.category_list.find((item)=>item.id == 0)
					source = 1
				}
				uni.showLoading({
					title: '飞速加载中...'
				})
				const res = await this.$http({
					url: '/addons/shop/api.goods/search_goods',
					data: {
						title:this.search_title,
						source:source
					}
				})
				uni.hideLoading()
				if (res.code == 1) {
					this.goods_list = res.data.goods_list
				}else{
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
				}
			},
			categoryChoose(item){
				this.category_choose = item
				this.goods_list = item.goods
				this.search_title = ''
				if(item.id == 0){
					return this.search_goods()
				}
			},
			
			goGoodsDetail(item){
				uni.navigateTo({
					url:`/pages/goods/details?goods_id=${item.id}`
				})
			},
			goGoodsEdit(item){
				uni.navigateTo({
					url:`/pages/goods/edit?goods_id=${item.id}`
				})
			},
		}
			
	}
</script>

<style lang="scss" scoped>
	.top_box{
		position: relative;
		width: 100%;
		height: 90%;
		display: flex;
		align-items: center;
		
		.left{
			margin: 0 20rpx;
			margin-left: 40rpx;
			height: 100%;
			width: 25%;
			image{
				margin: 5% 0;
				width: 100%;
				height: 80%;
			}
		}
		.right{
			margin: 5% 0;
			height: 90%;
			display: flex;
			align-items: center;
			border: 2rpx solid rgb(54,48,52);
			width: 60%;
			border-radius: 10rpx;
			
			input{
				margin: 0 12rpx;
			}
			.iconfont{
				margin: 0 12rpx;
			}
		}
	}
	
	.content{
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		display: flex;
		
		.left{
			width: 28%;
			height: 100%;
			overflow: scroll;
			
			.class_item{
				margin: 30rpx 0;
				
				.class_item_name{
					text-align: center;
					margin: 16rpx 0;
				}
				.line{
					margin: 0 38%;
					width: 24%;
					height: 6rpx;
					background-color: #eee;
				}
			}
			
			.class_item_check{
				transition: all 0.3s;
				font-weight: bold;
				transform: scale(1.2);
			}
			.class_line_check{
				transition: all 0.3s;
				background-color: $theme-color !important;
			}
			
		}
		
		.right{
			width: 72%;
			height: 100%;
			overflow: scroll;
			background-color: rgb(250,250,252);
			
			.class_name{
				margin: 20rpx 0 0 20rpx;
				font-weight: bold;
				font-size: 30rpx;
			}
			
			.goods_item{
				width: 46%;
				margin: 20rpx 2%;
				background-color: white;
				box-sizing: border-box;
				padding: 20rpx;
				
				.goods_img{
					width: 100%;
					image{
						width: 100%;
					}
				}
				
				.goods_name{
					word-break: break-all;
					margin: 12rpx 0;
					font-size: 26rpx;
				}
				
				.goods_price{
					font-size: 24rpx;
				}
				
				.iconfont{
				}
			}
		}
	}
</style>